﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw
{
    public partial class ToolControl : UserControl
    {
        private List<WidgetLibInfo> m_widgetLibs = new List<WidgetLibInfo>();
        private List<WidgetLibInfo> m_usingWidgetLibs = new List<WidgetLibInfo>();
        private List<SpaceTemplate> m_templates = new List<SpaceTemplate>();
        private List<int> m_usingWidgetIDs = new List<int>();

        public ToolControl()
        {
            InitializeComponent();
        }

        public void Init()
        {
            m_widgetLibs.Clear();
            m_usingWidgetLibs.Clear();
            m_templates.Clear();
            m_usingWidgetIDs.Clear();

            //widgetLibs
            foreach (WidgetLibInfo wli in OperationManager.Instance.Setting.WidgetLibList)
            {
                m_widgetLibs.Add(wli);
            }
            listLibs.ItemsSource = m_widgetLibs.ToArray();

            //usingWidgets
            foreach (KeyValuePair<int, WidgetLibInfo> wli in OperationManager.Instance.WidgetLibs)
            {
                m_usingWidgetIDs.Add(wli.Key);
                m_usingWidgetLibs.Add(wli.Value);
            }
            listWidgets.ItemsSource = m_usingWidgetLibs.ToArray();

            //templates
            foreach (SpaceTemplate st in TemplateConfiguration.Instance.Templates)
            {
                m_templates.Add(st);
            }
            ListTemplates.ItemsSource = m_templates.ToArray();

            AddPanel.Visibility = Visibility.Collapsed;
            listWidgets.Visibility = Visibility.Collapsed;
            listLibs.Visibility = Visibility.Visible;
            ListTemplates.Visibility = Visibility.Collapsed;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (ListTemplates.Visibility == Visibility.Visible) // for templates
            {
                AddTemplate();
            }
            else // for widget
            {
                AddWidget();
            }
        }

        private void AddTemplate()
        {
            //must check if in owner space
            if (ListTemplates.SelectedItem != null &&
                OperationManager.Instance.IsInMySpace)
            {
                SpaceTemplate st = m_templates[ListTemplates.SelectedIndex];
                foreach (SpaceWidget sw in st.Widgets)
                {
                    foreach (WidgetLibInfo wli in OperationManager.Instance.Setting.WidgetLibList)
                    {
                        if (wli.WidgetFileName == sw.File &&
                            wli.WidgetAssemblyName == sw.Class)
                        {
                            WidgetInfo wi = new WidgetInfo
                            {
                                WidgetID = -1,
                                WidgetLibInfoID = wli.ID,
                                Height = 0,
                                Width = 0,
                                OwnerSpaceID = OperationManager.Instance.MySpace.SpaceID,
                                PosX = sw.PosX,
                                PosY = sw.PosY,
                                PosZ = 0
                            };
                            OperationManager.Instance.AddWidgetInfo(wi);
                            break;
                        }
                    }
                }
                OperationManager.Instance.CurrentSpaceManager.ShowModalDialog(null);
            }
        }

        private void AddWidget()
        {
            //must check if in owner space
            if (listLibs.SelectedItem != null && OperationManager.Instance.IsInMySpace)
            {
                WidgetLibInfo wli = (WidgetLibInfo)listLibs.SelectedItem;
                WidgetInfo wi = new WidgetInfo
                {
                    WidgetID = -1,
                    WidgetLibInfoID = wli.ID,
                    Height = 0,
                    Width = 0,
                    OwnerSpaceID = OperationManager.Instance.MySpace.SpaceID,
                    PosX = 0,
                    PosY = 0,
                    PosZ = 0
                };
                OperationManager.Instance.AddWidgetInfo(wi);
                OperationManager.Instance.CurrentSpaceManager.ShowModalDialog(null);
            }
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            //must check if in owner space
            if (listWidgets.SelectedItem != null && OperationManager.Instance.IsInMySpace)
            {
                int widgetID = m_usingWidgetIDs[listWidgets.SelectedIndex];
                
                OperationManager.Instance.DelWidgetInfo(widgetID);
                OperationManager.Instance.CurrentSpaceManager.ShowModalDialog(null);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            AddPanel.Visibility = Visibility.Collapsed;
        }

        private void btnAddPanel_Click(object sender, RoutedEventArgs e)
        {
            listWidgets.Visibility = Visibility.Collapsed;
            listLibs.Visibility = Visibility.Visible;
            ListTemplates.Visibility = Visibility.Collapsed;
        }

        private void btnManagePanel_Click(object sender, RoutedEventArgs e)
        {
            listWidgets.Visibility = Visibility.Visible;
            listLibs.Visibility = Visibility.Collapsed;
            ListTemplates.Visibility = Visibility.Collapsed;
        }

        private void listWidgets_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (listWidgets.SelectedItem == null)
                return;

            WidgetLibInfo wli = m_usingWidgetLibs[listWidgets.SelectedIndex];
            try
            {
                imgWidget.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(wli.ImageUrl));
            }
            catch { }
            txtTitle.Text = wli.WidgetName;
            txtDesc.Text = wli.Description;
            btnAdd.Visibility = Visibility.Collapsed;
            btnDel.Visibility = Visibility.Visible;
            AddPanel.Visibility = Visibility.Visible;
        }

        private void listLibs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (listLibs.SelectedItem == null)
                return;

            WidgetLibInfo wli = m_widgetLibs[listLibs.SelectedIndex];
            try
            {
                imgWidget.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(wli.ImageUrl));
            }
            catch { }
            txtTitle.Text = wli.WidgetName;
            txtDesc.Text = wli.Description;
            btnAdd.Visibility = Visibility.Visible;
            btnDel.Visibility = Visibility.Collapsed;
            AddPanel.Visibility = Visibility.Visible;
        }

        private void BtnTemplatePanel_Click(object sender, RoutedEventArgs e)
        {
            listWidgets.Visibility = Visibility.Collapsed;
            listLibs.Visibility = Visibility.Collapsed;
            ListTemplates.Visibility = Visibility.Visible;
        }

        private void ListTemplates_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ListTemplates.SelectedItem == null)
                return;

            SpaceTemplate st = m_templates[ListTemplates.SelectedIndex];
            try
            {
                imgWidget.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(st.ImageUrl));
            }
            catch { }
            txtTitle.Text = st.Name;
            txtDesc.Text = st.Description;
            btnAdd.Visibility = Visibility.Visible;
            btnDel.Visibility = Visibility.Collapsed;
            AddPanel.Visibility = Visibility.Visible;
        }
    }
}
